package com.parammatch.demo;

import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class ParamMatchDemoT {
    /**
     * 参数配置
     */
    private List<Param> paramList;
    /**
     * 业务数据
     */
    private BusinessData businessData;

    public static void main(String[] args) {
        //构造数据
        List<Param> paramList = new ArrayList<Param>();
        paramList.add(new Param("param1",1));
        paramList.add(new Param("param2",2));
        paramList.add(new Param("param3",3));
        BusinessData businessData = new BusinessData(1,2,3,4,5);

        ParamMatchDemoT paramMatchDemoT = new ParamMatchDemoT();
        paramMatchDemoT.setParamList(paramList);
        paramMatchDemoT.setBusinessData(businessData);
        try {
            boolean result = paramMatchDemoT.matchParam();
            if(result){
                System.out.println("参数匹配成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 通用匹配方法
     * @return
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public boolean matchParam() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        boolean result = true;
        for(int i =0;i<paramList.size();i++){
            String paramType = paramList.get(i).getType();
            Method method = this.getClass().getDeclaredMethod("match"+paramType,Param.class);
            Boolean tempResult = (Boolean)method.invoke(this,paramList.get(i));
            if(!tempResult){
                result = false;
                break;
            }
        }
        return result;
    }

    /**
     * param1具体匹配逻辑
     * @param param
     * @return
     */
    public boolean matchparam1(Param param){
        Object paramValue= param.getValue();
        Object businessValue= businessData.getValue1();
        if(paramValue!= null){
            System.out.println(param.getType()+"匹配成功");
            return true;
        }
        return false;
    }
    /**
     * param2具体匹配逻辑
     * @param param
     * @return
     */
    private boolean matchparam2(Param param){
        Object paramValue= param.getValue();
        Object businessValue= businessData.getValue2();
        if(paramValue!= null){
            //自定义匹配规则
            System.out.println(param.getType()+"匹配成功");
            return true;
        }
        return false;
    }
    /**
     * param3具体匹配逻辑
     * @param param
     * @return
     */
    private boolean matchparam3(Param param){
        Object paramValue= param.getValue();
        Object businessValue= businessData.getValue3();
        if(paramValue!= null){
            //自定义匹配规则
            System.out.println(param.getType()+"匹配成功");
            return true;
        }
        return false;
    }
    /**
     * param4具体匹配逻辑
     * @param param
     * @return
     */
    private boolean matchparam4(Param param){
        Object paramValue= param.getValue();
        Object businessValue= businessData.getValue4();
        if(paramValue!= null){
            //自定义匹配规则
            System.out.println(param.getType()+"匹配成功");
            return true;
        }
        return false;
    }
    /**
     * param5具体匹配逻辑
     * @param param
     * @return
     */
    private boolean matchparam5(Param param){
        Object paramValue= param.getValue();
        Object businessValue= businessData.getValue5();
        if(paramValue!= null){
            //自定义匹配规则
            System.out.println(param.getType()+"匹配成功");
            return true;
        }
        return false;
    }
}
